package com.wharf.base.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;

/**登录拦截器*/
public class LoginInterceptor implements HandlerInterceptor{
	
	@Override
	/**拦截器只能返回boolean类型值，true通过（不拦截），false拦截*/
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String path = request.getServletPath();//用户请求的接口地址
		
		String[] filter = {"/back/userlogin","/back/adlogin","/back/error/notLogin","/back/addUser"};//不需要拦截的地址
		
		for(String url:filter) {
			if(path.equals(url)) {
				return true;//返回true表示不拦截
			}
		}
		
		//判断登录状态
		HttpSession httpSession = request.getSession();
		String loginUser = (String) httpSession.getAttribute(AuthTool.LOGIN_STATUS);
		if(null==loginUser) {
			request.getRequestDispatcher("/back/error/notLogin").forward(request, response);
			return false;
		}else {
			return true;
		}
	}

}
